package com.tnsw.noxnox;

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

import android.content.ClipData;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.DragEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnDragListener;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.Button;
import android.widget.ImageView;

public class MyScrollView extends Fragment implements OnGlobalLayoutListener, OnTouchListener, OnClickListener, OnLongClickListener, OnDragListener {
	
	private float startX, startY, diffX, diffY;
	private float dragX, dragY;
	private String logName = "MyScrollView";
	private ViewGroup root, signupView;
	private ViewPagerInterceptable mParentViewPager;
	
	private ImageView imgAndroid, imgPic1;
	private CustomFrameLayout scrollLayout;
	
	public MyScrollView() {
		Log.d(logName, "constructor()");
	}
	
	public MyScrollView(ViewPagerInterceptable parentViewPager) {
		Log.d(logName, "constructor(parentViewPager)");
		this.mParentViewPager = parentViewPager;
		
	}
	
	public static Fragment newInstance(Context context, ViewPagerInterceptable parentViewPager) {
		Log.d("MyScrollView", "newInstance");
		MyScrollView f = new MyScrollView(parentViewPager);
		
		return(f);
	}
	
	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		Log.d(logName, "onCreateView()");
		this.root = (ViewGroup) inflater.inflate(R.layout.myscroll1_layout, null);		
		
		Log.d(logName, "root.toString()=" + this.root.toString());
		
		this.root.setOnDragListener(this);
		this.root.setOnTouchListener(this);
		this.root.getViewTreeObserver().addOnGlobalLayoutListener(this);
		
		this.scrollLayout = (CustomFrameLayout) this.root.findViewById(R.id.scrollLayout);
		
		this.imgAndroid = (ImageView) this.root.findViewById(R.id.imgAndroid);
		this.imgPic1 = (ImageView) this.root.findViewById(R.id.imgPic1);
		
		this.imgAndroid.setOnLongClickListener(this);
		this.imgAndroid.setOnClickListener(this);
		this.imgAndroid.setOnDragListener(this);
		this.imgAndroid.setOnTouchListener(this);	
		//this.imgAndroid.setLeft(40);		// does not work here
		
		Log.d(logName, "onCreateView() - imgAndroid.getLeft()=" + String.valueOf(this.imgAndroid.getLeft()));
		
		this.imgPic1.setOnLongClickListener(this);
		this.imgPic1.setOnDragListener(this);
		this.imgPic1.setOnClickListener(this);
		//this.imgPic1.setOnTouchListener(this);
		
		this.imgPic1.setTag("imgPic1");
	
		//this.imgAndroid.setVisibility(View.INVISIBLE);
		//this.imgPic1.setVisibility(View.INVISIBLE);

		
		return(root);
		
		
	}
	
	@Override
	public void onGlobalLayout() {
		Log.d(logName, "onGlobalLayout() - imgAndroid.getLeft()=" + String.valueOf(this.imgAndroid.getLeft()));
		this.imgAndroid.setLeft(40);
		this.imgAndroid.setTop(40);
		this.imgAndroid.setRight(this.imgAndroid.getRight()+40);
		this.imgAndroid.setBottom(this.imgAndroid.getBottom()+40);
	
		Log.d(logName, "this.scrollLayout.getRight()=" + this.scrollLayout.getRight());
		Log.d(logName, "this.scrollLayout.getBottom()=" + this.scrollLayout.getBottom());
		
	}

	@Override
	public View getView() {
		Log.d(logName, "getView() is called");
		return(super.getView());
	}
	
	@Override
	public boolean onLongClick(View v) {

		if (v == this.imgAndroid) {
			Log.d(logName, "imgAndroid long click");
			
		} else if (v == this.imgPic1) {
			Log.d(logName, "imgPic1 long click");
			ClipData.Item item = new ClipData.Item((String) v.getTag());
			ClipData dragData = new ClipData((String) v.getTag(), new String[] {"text/plain"}, item);
			View.DragShadowBuilder shadow = new MyDragShadowBuilder(this.imgPic1);
			//View.DragShadowBuilder shadow = new View.DragShadowBuilder(this.imgPic1);

			this.imgPic1.startDrag(dragData, shadow, null, 0);
			
			Log.d(logName, "started Drag");
		}
		
		return(true);
	}

	@Override
	public boolean onDrag(View v, DragEvent event) {
		//Log.d(logName, "onDrag()");

		if (v instanceof ImageView) {
			
			float x, y;
			x = event.getX();
			y = event.getY();
			
			if (v == this.imgPic1) {
				switch (event.getAction()) {
				case DragEvent.ACTION_DRAG_STARTED:
					this.dragX = x;
					this.dragY = y;
					
					this.startX = x;
					this.startY = y;
					this.diffX = this.startX - v.getLeft();
					this.diffY = this.startY - v.getTop();
					
					Log.d(logName, "ACTION_DRAG_STARTED start(" + String.valueOf(this.startX) + ", " + String.valueOf(this.startY) + ") (diffX=" + String.valueOf(this.diffX) + ", diffY=" + String.valueOf(this.diffY) + ")");
						
					break;
					
				case DragEvent.ACTION_DRAG_ENTERED:
					Log.d(logName, "ACTION_DRAG_ENTERED");
					break;
					
				case DragEvent.ACTION_DRAG_LOCATION:		
					this.dragX = x;
					this.dragY = y;
					Log.d(logName, "ACTION_DRAG_LOCATION (" + String.valueOf(x) + ", " + String.valueOf(y) + ") start(" + String.valueOf(this.startX) + ", " + String.valueOf(this.startY) + ")");

					break;
					
				case DragEvent.ACTION_DRAG_ENDED:
					
					
					int newX = v.getLeft() + ((int) (this.dragX-this.diffX));
					int newY = (int) (this.dragY-this.diffY);
					
					Drawable d = ((ImageView) v).getDrawable();
					BitmapDrawable bd = (BitmapDrawable) d;
					int width = bd.getBitmap().getWidth();
					int height = bd.getBitmap().getHeight();
					
					newX = (int) (this.dragX - (width/2));
					
					//v.setLeft(newX);
					//v.setTop(newY);
					Log.d(logName, "ACTION_DRAG_ENDED new(" + String.valueOf(newX) + ", " + String.valueOf(newY) + ")");
					break;
					
				case DragEvent.ACTION_DRAG_EXITED:
					Log.d(logName, "ACTION_DRAG_EXITED (" + String.valueOf(x) + ", " + String.valueOf(y) + ")");
					break;
					
				case DragEvent.ACTION_DROP:
					this.dragX = x;
					this.dragY = y;
					Log.d(logName, "ACTION_DROP (" + String.valueOf(x) + "," + String.valueOf(y) + ")");
					break;
				}
			}
		}
		
		return true;
	}
	
	@Override
	public void onClick(View v) {
		if (v == this.imgAndroid) {
			Log.d(logName, "(x,y)=" + this.imgAndroid.getLeft() + ", " + this.imgAndroid.getTop());
			
			boolean b = this.mParentViewPager.getDisablePaging();
			if (b == true) {
				this.mParentViewPager.setDisablePaging(false);
				Log.d(logName, "disablePaging=false");
			} else {
				this.mParentViewPager.setDisablePaging(true);
				Log.d(logName, "disablePaging=true");
			}
			

		}
		
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		float x, y;
		x = event.getX();
		y = event.getY();
		
		if (v == this.root) {
			Log.d(logName, "root is touch");
		} else if (v == this.imgAndroid) {
			Log.d(logName, "imgAndroid is touch");
			/*
			FragmentTransaction tran  = this.getFragmentManager().beginTransaction();
			tran.detach(this);
			tran.attach(this);
			tran.commit();
			*/
			
		} else if (v == this.imgPic1) {
			Log.d(logName, "imgPic1 is touch");
		}
		
		return(true);
	}
	

	
	
}
